home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / regagnt / regcia.cls < prev   
Text File  |  1996-01-16  |  11KB  |  400 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = 0   'False
  4. END
  5. Attribute VB_Name = "clsRegistryAgent"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = False
  8. '******To SET registry values*******
  9. 'Dim tTempKey            As String
  10. '
  11. 'Set RegistryAgent = New clsRegistryAgent
  12. '
  13. ' Use the class properties and methods to load
  14. ' some test data into the registry
  15. 'tTempKey = "\YourKey\Anotherkey"
  16. 'RegistryAgent.RegistryKey = tTempKey
  17. 'RegistryAgent.SubKey = "Data"
  18. 'RegistryAgent.KeyValue = 399
  19. 'RegistryAgent.SetValue
  20. '******To GET Registry values*****
  21. 'Dim tTempKey            As String
  22. 'Dim TipVal As String
  23. 'Set RegistryAgent = New clsRegistryAgent
  24. '
  25. ' Use the class properties and methods to load
  26. ' some test data into the registry
  27. 'tTempKey = "\YourKey\Anotherkey"
  28. 'RegistryAgent.RegistryKey = tTempKey
  29. 'RegistryAgent.SubKey = "Data"
  30. 'RegistryAgent.GetValue
  31. Option Explicit
  32.  
  33. ' Public properties
  34. Dim ptRegistryKey       As String
  35. Dim ptSubKey            As String
  36. Dim ptKeyValue          As String
  37. Dim plStatus            As Long
  38.  
  39. Const HKEY_CLASSES_ROOT = &H80000000
  40. Const HKEY_CURRENT_USER = &H80000001
  41. Const HKEY_LOCAL_MACHINE = &H80000002
  42. Const HKEY_USERS = &H80000003
  43. Const HKEY_DYN_DATA = &H80000004
  44.  
  45. Const REG_SZ = 1
  46.  
  47. ' Registry API prototypes
  48. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
  49.     (ByVal hkey As Long, _
  50.      ByVal lpSubKey As String, _
  51.      phkResult As Long) As Long
  52.      
  53. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  54.     (ByVal hkey As Long, _
  55.      ByVal lpSubKey As String) As Long
  56.      
  57. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  58.     (ByVal hkey As Long, _
  59.      ByVal lpSubKey As String) As Long
  60.      
  61. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
  62.     (ByVal hkey As Long, _
  63.      ByVal lpValueName As String, _
  64.      ByVal lpReserved As Long, _
  65.      lpType As Long, _
  66.      lpData As Any, _
  67.      lpcbData As Long) As Long
  68.      
  69. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
  70.     (ByVal hkey As Long, _
  71.      ByVal lpValueName As String, _
  72.      ByVal Reserved As Long, _
  73.      ByVal dwType As Long, _
  74.      lpData As Any, _
  75.      ByVal cbData As Long) As Long
  76.  
  77. ' Registry error constants
  78. Const ERROR_SUCCESS = 0&
  79. Const ERROR_BADDB = 1009&
  80. Const ERROR_BADKEY = 1010&
  81. Const ERROR_CANTOPEN = 1011&
  82. Const ERROR_CANTREAD = 1012&
  83. Const ERROR_CANTWRITE = 1013&
  84. Const ERROR_REGISTRY_RECOVERED = 1014&
  85. Const ERROR_REGISTRY_CORRUPT = 1015&
  86. Const ERROR_REGISTRY_IO_FAILED = 1016&
  87. Const ERROR_NOT_REGISTRY_FILE = 1017&
  88. Const ERROR_KEY_DELETED = 1018&
  89. Const ERROR_NO_LOG_SPACE = 1019&
  90. Const ERROR_KEY_HAS_CHILDREN = 1020&
  91. Const ERROR_CHILD_MUST_BE_VOLATILE = 1021&
  92. Const ERROR_RXACT_INVALID_STATE = 1369&
  93.  
  94. ' Private error codes
  95. Const REGAGENT_NOKEY = -1002
  96. Const REGAGENT_NOSUBKEY = -1003
  97.  
  98. Public Sub CreateKey()
  99.  
  100. Dim lResult         As Long
  101.  
  102. plStatus = 0            ' Assume succcess
  103.  
  104. ' Make sure all required properties have been set
  105. If Len(ptRegistryKey) = 0 Then
  106.     ' The key property is not set, so flag an error
  107.     plStatus = REGAGENT_NOKEY
  108.     Exit Sub
  109. End If
  110.  
  111. ' Make the call to create the key
  112. plStatus = RegCreateKey(HKEY_LOCAL_MACHINE, ptRegistryKey, lResult)
  113.            
  114. End Sub
  115.  
  116. Public Sub DeleteKey()
  117.  
  118. Dim lKeyId          As Long
  119.  
  120. plStatus = 0            ' Assume succcess
  121.  
  122. ' Make sure all required properties have been set
  123. If Len(ptRegistryKey) = 0 Then
  124.     ' The key property is not set, so flag an error
  125.     plStatus = REGAGENT_NOKEY
  126.     Exit Sub
  127. End If
  128. If Len(ptSubKey) = 0 Then
  129.     ' The sub key property is not set, so flag an error
  130.     plStatus = REGAGENT_NOSUBKEY
  131.     Exit Sub
  132. End If
  133.  
  134. ' Open the key by attempting to create it. If it
  135. ' already exists we get back an ID.
  136. plStatus = RegCreateKey(HKEY_LOCAL_MACHINE, ptRegistryKey, lKeyId)
  137. If plStatus = 0 Then
  138.     ' We get a key ID so we can delete the entry
  139.     plStatus = RegDeleteKey(lKeyId, ByVal ptSubKey)
  140. End If
  141.  
  142. End Sub
  143.  
  144. Public Sub DeleteValue()
  145.  
  146. Dim lKeyId          As Long
  147.  
  148. plStatus = 0            ' Assume succcess
  149.  
  150. ' Make sure all required properties have been set
  151. If Len(ptRegistryKey) = 0 Then
  152.     ' The key property is not set, so flag an error
  153.     plStatus = REGAGENT_NOKEY
  154.     Exit Sub
  155. End If
  156. If Len(ptSubKey) = 0 Then
  157.     ' The sub key property is not set, so flag an error
  158.     plStatus = REGAGENT_NOSUBKEY
  159.     Exit Sub
  160. End If
  161.  
  162. ' Open the key by attempting to create it. If it
  163. ' already exists we get back an ID.
  164. plStatus = RegCreateKey(HKEY_LOCAL_MACHINE, ptRegistryKey, lKeyId)
  165. If plStatus = 0 Then
  166.     ' We got a key ID so we can delete the value
  167.     plStatus = RegDeleteValue(lKeyId, ByVal ptSubKey)
  168. End If
  169.  
  170. End Sub
  171.  
  172. Public Function GetErrorText() As String
  173.  
  174. ' Evaluate the status property value and return the
  175. ' associated error message text.
  176.  
  177. Select Case plStatus
  178.   Case REGAGENT_NOKEY
  179.     GetErrorText = "You have not provided a registry key."
  180.     
  181.   Case REGAGENT_NOSUBKEY
  182.     GetErrorText = "You have not provided a sub key."
  183.     
  184.   Case ERROR_BADDB
  185.     GetErrorText = "The configuration registry database is corrupt."
  186.     
  187.   Case ERROR_BADKEY
  188.     GetErrorText = "The configuration registry key is invalid."
  189.     
  190.   Case ERROR_CANTOPEN
  191.     GetErrorText = "The configuration registry key could not be opened."
  192.     
  193.   Case ERROR_CANTREAD
  194.     GetErrorText = "The configuration registry key could not be read."
  195.     
  196.   Case ERROR_CANTWRITE
  197.     GetErrorText = "The configuration registry key could not be written."
  198.     
  199.   Case ERROR_REGISTRY_RECOVERED
  200.     GetErrorText = "One of the files in the Registry database had to be recovered " & _
  201.                    "by use of a log or alternate copy. The recovery was successful."
  202.                    
  203.   Case ERROR_REGISTRY_CORRUPT
  204.     GetErrorText = "The Registry is corrupt. The structure of one of the files that contains " & _
  205.                    "Registry data is corrupt, or the system's image of the file in memory " & _
  206.                    "is corrupt, or the file could not be recovered because the alternate " & _
  207.                    "copy or log was absent or corrupt."
  208.                    
  209.   Case ERROR_REGISTRY_IO_FAILED
  210.     GetErrorText = "An I/O operation initiated by the Registry failed unrecoverably. " & _
  211.                    "The Registry could not read in, or write out, or flush, one of the files " & _
  212.                    "that contain the system's image of the Registry."
  213.                    
  214.   Case ERROR_NOT_REGISTRY_FILE
  215.     GetErrorText = "The system has attempted to load or restore a file into the Registry, but the " & _
  216.                    "specified file is not in a Registry file format."
  217.                    
  218.   Case ERROR_KEY_DELETED
  219.     GetErrorText = "Illegal operation attempted on a Registry key which has been marked for deletion."
  220.     
  221.   Case ERROR_NO_LOG_SPACE
  222.     GetErrorText = "System could not allocate the required space in a Registry log."
  223.     
  224.   Case ERROR_KEY_HAS_CHILDREN
  225.     GetErrorText = "Cannot create a symbolic link in a Registry key that already " & _
  226.                    "has subkeys or values."
  227.                    
  228.   Case ERROR_CHILD_MUST_BE_VOLATILE
  229.     GetErrorText = "Cannot create a stable subkey under a volatile parent key."
  230.     
  231.   Case ERROR_RXACT_INVALID_STATE
  232.     GetErrorText = "The transaction state of a Registry subtree is incompatible with the " & _
  233.                    "requested operation."
  234.                    
  235. End Select
  236.  
  237. End Function
  238.  
  239. Public Sub GetValue()
  240.  
  241. Dim lResult             As Long
  242. Dim lKeyId              As Long
  243. Dim tKeyValue           As String
  244. Dim lBufferSize         As Long
  245.  
  246. plStatus = 0            ' Assume succcess
  247.  
  248. ' Make sure all required properties have been set
  249. If Len(ptRegistryKey) = 0 Then
  250.     ' The key property is not set, so flag an error
  251.     plStatus = REGAGENT_NOKEY
  252.     Exit Sub
  253. End If
  254. If Len(ptSubKey) = 0 Then
  255.     ' The sub key property is not set, so flag an error
  256.     plStatus = REGAGENT_NOSUBKEY
  257.     Exit Sub
  258. End